const BaseUrl = "http://118.24.239.63:3001/api";

const request = {
  get(url: string, params: any = {}) {
    const str = Object.keys(params)
      .map((item: string) => {
        return `${item}=${params[item]}`;
      })
      .join("&");

    url += "?" + str;

    return fetch(`${BaseUrl}${url}`, {
      headers: {
        token: localStorage.getItem("token") || "",
        "content-type": "application/json",
      },
    })
      .then((response) => response.json())
      .then((res) => {
        if (res.success) {
          return res;
        }
      });
  },
  post(url: string, data: any = {}) {
    return fetch(`${BaseUrl}${url}`, {
      method: "POST",
      headers: {
        token: localStorage.getItem("token") || "",
        "content-type": "application/json",
      },
      body: JSON.stringify(data),
    })
      .then((response) => response.json())
      .then((res) => {
        if (res.success) {
          return res;
        }
      });
  },
};

export default request;
